function  $(id){
    return document.getElementById(id);
}

window.onload = function (){
    findByItem(1);
}

/**
 * 动态条件分页查询
 * @param pageNO 页码
 */
function  findByItem(pageNO){
    var str = "";
    axios.get("/goods/findByItem",{
        params: {
            pageNO,
            name:$("serchName").value,
            startDate:$("startDate").value,
            endDate:$("endDate").value
        }
    }).then(resp =>{
        var info = resp.data;

        var str = "";
        info.records.forEach(n =>{
            str += `<tr><td>${n.id}</td><td>${n.name}</td>
                <td>${n.createDate}</td><td>${n.price}</td>
                <td><input type="button" value="删除" onclick="del(${n.id})">
                <input type="button" value="修改" onclick="findById(${n.id})"></td></tr>`;
        })
        $("data").innerHTML = str;

        var cutStr = "";
        for(var i=1;i<=info.pages;i++){
            cutStr += `<a style="margin-left: 10px" 
                href="javascript:findByItem(${i})">${i}</a>`;
        }
        $("cutPageDiv").innerHTML = cutStr;
    })

}

function findById(id){
    $("updateDiv").style.visibility = "visible";
    axios.get("/goods/findById",{
        params:{id}
    }).then(resp =>{
        var info = resp.data;
        $("goodId").value = info.id;
        $("nameSpan").innerHTML = info.name;
        $("createDateSpan").innerHTML = info.createDate;
        $("updatePrice").value = info.price;
    })
}

function updateGoods(){
    axios.get("/goods/update",{
        params:{
            id:$("goodId").value,
            price: $("updatePrice").value
        }
    }).then(resp =>{
        if(resp.data == "ok"){
            $("updateDiv").style.visibility = "hidden";
            findByItem(1);
        }

    })
}

/**
 * 删除
 * @param id 商品ID
 */
function del(id){
    axios.get("/goods/del",{
        params:{id}
    }).then(resp =>{
        if(resp.data == "ok"){
            findByItem(1);
        }
    })
}

/**
 * 添加商品
 */
function addGoods(){
    axios.get("/goods/add",{
        params: {
            name:$("addName").value,
            createDate:$("addCreateDate").value,
            price:$("addPrice").value
        }
    }).then(resp =>{
       if(resp.data == "ok"){
           findByItem(1);
           $("addDiv").style.visibility = "hidden";
           $("addName").value = "";
           $("addCreateDate").value="";
           $("addPrice").value = "";
       }
    });
}